package com.learn.xml_config.basic_label;

import com.learn.xml_config.basic_label.beans.dao.UserDao;
import com.learn.xml_config.basic_label.beans.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BasicLabel {
    public static void main(String[] args) {
        // property/constructor-arg：观察输出的赋值情况
        labelPropertyConstructorarg();
        System.out.println("===============");
        
        // beans-profile
        propertyProfile();
        System.out.println("===============");
        
        // import
        labelImport();
    }
    
    public static void labelPropertyConstructorarg() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("xml-config/basic-label/property-constructor-arg.xml");
        UserService userService = applicationContext.getBean("userService", UserService.class);
    }
    
    public static void propertyProfile() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("xml-config/basic-label/beans-profile.xml");
        System.out.println(applicationContext.getBean("userDao", UserDao.class));
        
        // 设置激活配置，另一种方式设置虚拟机参数-Dspring.profiles.active=xxx
        System.setProperty("spring.profiles.active", "dev");
        ApplicationContext applicationContext1 = new ClassPathXmlApplicationContext("xml-config/basic-label/beans-profile.xml");
        System.out.println(applicationContext1.getBean("userDao", UserDao.class)); // 不报错表示默认环境仍然生效
        System.out.println(applicationContext1.getBean("userDao(dev)", UserDao.class));
        
        // 设置激活配置，另一种方式设置虚拟机参数-Dspring.profiles.active=xxx
        System.setProperty("spring.profiles.active", "test");
        ApplicationContext applicationContext2 = new ClassPathXmlApplicationContext("xml-config/basic-label/beans-profile.xml");
        System.out.println(applicationContext2.getBean("userDao", UserDao.class)); // 不报错表示默认环境仍然生效
        System.out.println(applicationContext2.getBean("userDao(test)", UserDao.class));
    }
    
    public static void labelImport() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("xml-config/basic-label/import/import.xml");
        System.out.println(applicationContext.getBean("userDao(import)", UserDao.class));
    }
}
